﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BaiTu.Framework;
using System.IO;
using BaiTu.Framework;

namespace BaiTu.Common.Helpers
{
    /// <summary>
    /// 文件帮助类
    /// </summary>
    public sealed class FileHelper : Framework.Interfaces.ISingletonDependency
    {
        /// <summary>
        /// /加锁 
        /// </summary>
        private static readonly object _lock = new object();
        public static FileInfo GetFileInfo(string fileName)
        {
            if (FileExists(fileName).Success == false)
            {
                throw new Framework.Exceptions.BaiTuCoreException(string.Format("文件不存在，路径为:{0}", fileName));
            }
            FileInfo fileInfo = new FileInfo(fileName);
            return fileInfo;
        }

        public static InnerResult Write(string fileName, string value)
        {
            InnerResult innerResult = new Framework.InnerResult();
            if (!(FileExists(fileName).Success))
            {
                innerResult = CreateTextFile(fileName);
                if (innerResult.Success == false)
                {
                    return innerResult;
                }
            }
            lock (_lock)
            {
                using (StreamWriter writer = File.AppendText(fileName))
                {
                    writer.WriteLine("{0} : {1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), value);
       

                    innerResult.Success = true;
                    if (writer != null)
                    {
                        writer.Flush();
                        writer.Close();
                        writer.Dispose();
                    }
                }
            }
            return innerResult;
        }
        /// <summary>
        /// 判断文件是否存在
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static InnerResult FileExists(string path)
        {
            InnerResult innerResult = new InnerResult();
            try
            {
                innerResult.Success = File.Exists(path);
            }
            catch (Exception ex)
            {
                innerResult = ex.GetInnerResult();
            }
            return innerResult;
        }
        /// <summary>
        /// 创建指定路径的文件  如果指定路径不存在  则创建路径
        /// </summary>
        /// <returns></returns>
        public static InnerResult CreateTextFile(string filePath)
        {
            InnerResult innerResult = new Framework.InnerResult();
            ///首先检测是否创建的文件 所在的目录存在
            if ((innerResult = CheckFileOfDirectory(filePath)).Success == false)
            {
                return innerResult;
            }
            innerResult.Clear();
            try
            {
                File.CreateText(filePath);
                innerResult.Success = true;
            }
            catch (Exception ex)
            {
                innerResult = ex.GetInnerResult(string.Format("创建文件失败，文件路径为:{0}", filePath));
            }
            return innerResult;
        }
        /// <summary>
        /// 创建文件 不限定为txt文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static InnerResult CreateFile(string filePath)
        {
            InnerResult innerResult = new Framework.InnerResult();
            if ((innerResult = CheckFileOfDirectory(filePath)).Success == false)
            {
                return innerResult;
            }
            innerResult.Clear();
            try
            {
                File.Create(filePath);
                innerResult.Success = true;
            }
            catch (Exception ex)
            {
                innerResult = ex.GetInnerResult(string.Format("创建文件失败，文件路径为:{0}", filePath));
            }
            return innerResult;
        }
        /// <summary>
        /// 删除指定文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static InnerResult DeleteFile(string filePath)
        {
            InnerResult innerResult = new Framework.InnerResult();
            try
            {
                File.Delete(filePath);
                innerResult.Success = true;
            }
            catch (Exception ex)
            {
                innerResult = ex.GetInnerResult(string.Format("删除文件失败，文件路径为:{0}", filePath));
            }
            return innerResult;
        }
        /// <summary>
        /// 检测指定的文件路径对应的目录是否存在
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        private static InnerResult CheckFileOfDirectory(string filePath)
        {
            InnerResult innerResult = new Framework.InnerResult()
            {
                Success = true
            };
            string dirName = Path.GetDirectoryName(filePath);
            if (!DirectoryHelper.DirectoryExists(dirName).Success)
            {
                InnerResult inner = new Framework.InnerResult();
                ///如果创建目录失败
                if ((inner = DirectoryHelper.CreateDirectory(dirName)).Success == false)
                {
                    return inner;
                }
            }
            return innerResult;
        }
        /// <summary>
        /// 获取文件的最后一次写入时间
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static DateTime GetLastWriteTime(string filePath)
        {
            return File.GetLastWriteTime(filePath);
        }
    }
}
